package com.group2.view;
import static model.netBase.IP;

import javax.swing.*;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.*;
import java.net.Socket;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import model.Student;
import com.group2.util.StringUtil;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

/*用户管理模块：
 * 管理员搜索学生并修改学生密码
 * 管理员修改自己的密码*/
public class UserManageFrame_Admin extends JFrame{
	
	private String currentName;
	
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JPanel jPanel1;
	private JPanel jPanel2;
	private JScrollPane jScrollPane1;
	private static ArrayList<Student> list;
	private JButton jb_modify;
	private JButton jb_search;
	private JButton jb_mymodify;
	private JTable passwordTable;
	private JTextField passwordTxt;
	private JTextField s_sidTxt;//查询学号
	private JTextField s_snameTxt;//查询姓名
	private JTextField snameTxt;
	private JTextField sidTxt;
	private Timer timer;

	
	UserManageFrame_Admin(String name) throws IOException, ClassNotFoundException {
		currentName=name;
		initComponents();
		int wW=this.getWidth();
		int wH=this.getHeight();
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		int sW=screenSize.width;
		int sH=screenSize.height;
		this.setLocation(sW/2-wW/2,sH/2-wH/2);
		reFresh();
	}
	private void reFresh() throws IOException, ClassNotFoundException {
		String id=s_sidTxt.getText();
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("68" +'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream in = new ObjectInputStream(is);
		Object obj = in.readObject();
		System.out.println("传输结束");
		list = (ArrayList<Student>) obj;
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
		fillTable(list);
	}
	
	//填充全校已注册学生学号+姓名，原密码对管理员不可见，管理员可以直接修改学生密码
	private void fillTable(ArrayList<Student> course) {
		DefaultTableModel dtm = (DefaultTableModel) passwordTable.getModel();
		dtm.setRowCount(0);
		try {
			int size = course.size();
			int index = 0;
			while (size > 0) {
				Vector v = new Vector();
				v.add(course.get(index).getNumber()) ;v.add(course.get(index).getName());
				dtm.addRow(v);
				size--;
				index++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

	
	private void initComponents() {
	jScrollPane1 = new JScrollPane();
	passwordTable = new JTable();
	jPanel1 = new JPanel();
	jLabel1 = new JLabel();
	jLabel4=new JLabel();
	s_sidTxt = new JTextField();
	s_snameTxt = new JTextField();
	jb_search = new JButton();
	jPanel2 = new JPanel();
	sidTxt = new JTextField();
	snameTxt=new JTextField();
	jLabel2 = new JLabel();
	jLabel3 = new JLabel();
	jLabel5=new JLabel();
	passwordTxt = new JTextField();
	jb_modify = new JButton();
	jb_mymodify=new JButton();

	setTitle("管理学生用户密码");

	passwordTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

	}, new String[] { "学生学号", "学生姓名" }) {
		boolean[] canEdit = new boolean[] { false, false };

		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return canEdit[columnIndex];
		}
	});
	passwordTable.addMouseListener(new java.awt.event.MouseAdapter() {
		public void mousePressed(java.awt.event.MouseEvent evt) {
			passwordTableMousePressed(evt);
		}
	});
	jScrollPane1.setViewportView(passwordTable);

	jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("搜索条件"));
	jLabel1.setText("学生学号：");
	jLabel4.setText("学生姓名：");

	jb_search.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/search.png")));
	jb_search.setText("查询");
	jb_search.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			try {
				jb_searchActionPerformed(evt);
			} catch (IOException e) {
				throw new RuntimeException(e);
			} catch (ClassNotFoundException e) {
				throw new RuntimeException(e);
			}
		}
	});

	javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	jPanel1Layout.setHorizontalGroup(
		jPanel1Layout.createParallelGroup(Alignment.TRAILING)
			.addGroup(jPanel1Layout.createSequentialGroup()
				.addContainerGap(76, Short.MAX_VALUE)
				.addComponent(jLabel1)
				.addGap(18)
				.addComponent(s_sidTxt, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
				.addGap(47)
				.addComponent(jLabel4)
				.addGap(18)
				.addComponent(s_snameTxt, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
				.addGap(54)
				.addComponent(jb_search)
				.addGap(51))
	);
	jPanel1Layout.setVerticalGroup(
		jPanel1Layout.createParallelGroup(Alignment.LEADING)
			.addGroup(jPanel1Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
					.addComponent(jLabel1)
					.addComponent(s_sidTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(jLabel4)
					.addComponent(s_snameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(jb_search))
				.addContainerGap(29, Short.MAX_VALUE))
	);
	jPanel1.setLayout(jPanel1Layout);

	jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("表单操作"));

	sidTxt.setEditable(false);
    snameTxt.setEditable(false);
	jLabel2.setText("学生学号：");
	jLabel3.setText("学生姓名：");
	jLabel5.setText("学生密码：");
	
	jb_modify.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/modify.png")));
	jb_modify.setText("修改学生密码");
	jb_modify.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			try {
				jb_modifyActionPerformed(evt);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	});
	
	jb_mymodify.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/modify.png")));
	jb_mymodify.setText("修改我的密码");
	jb_mymodify.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			jb_mymodifyActionPerformed(evt);
		}
	});

	javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
	jPanel2Layout.setHorizontalGroup(
		jPanel2Layout.createParallelGroup(Alignment.LEADING)
			.addGroup(jPanel2Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(jPanel2Layout.createParallelGroup(Alignment.TRAILING)
					.addGroup(jPanel2Layout.createSequentialGroup()
						.addComponent(jLabel2)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(sidTxt, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
						.addGap(18)
						.addComponent(jLabel3)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(snameTxt, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
						.addGap(30)
						.addComponent(jLabel5)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
						.addGap(42))
					.addGroup(jPanel2Layout.createSequentialGroup()
						.addComponent(jb_mymodify)
						.addGap(121)
						.addComponent(jb_modify)
						.addGap(147))))
	);
	jPanel2Layout.setVerticalGroup(
		jPanel2Layout.createParallelGroup(Alignment.LEADING)
			.addGroup(jPanel2Layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
					.addComponent(jLabel2)
					.addComponent(sidTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(21)
					.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(jLabel5)
					.addComponent(snameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(jLabel3))
				.addGap(18)
				.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
					.addComponent(jb_mymodify)
					.addComponent(jb_modify))
				.addContainerGap(34, Short.MAX_VALUE))
	);
	jPanel2.setLayout(jPanel2Layout);



	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	layout.setHorizontalGroup(
		layout.createParallelGroup(Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(jPanel1, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
							.addComponent(jScrollPane1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE))
						.addContainerGap(21, Short.MAX_VALUE))
					.addGroup(layout.createSequentialGroup()
						.addComponent(jPanel2, 0, 0, Short.MAX_VALUE)
						.addGap(21))))
	);
	layout.setVerticalGroup(
		layout.createParallelGroup(Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(ComponentPlacement.UNRELATED)
				.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
				.addGap(18)
				.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(71, Short.MAX_VALUE))
	);
	getContentPane().setLayout(layout);

	pack();
}

	//管理员修改自己的密码
	private void jb_mymodifyActionPerformed(java.awt.event.ActionEvent evt) {
		new UserPasswordModifyFrame(currentName,timer).setVisible(true);
	}
	
private void jb_modifyActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
	String sno = this.sidTxt.getText();
	//String sname=this.snameTxt.getText();
	String pwd =this.passwordTxt.getText();
	if (StringUtil.isEmpty(sno)) {
		JOptionPane.showMessageDialog(this, "请选择要修改的记录!");
		return;
	}
	
	String password = this.passwordTxt.getText();
	if (StringUtil.isEmpty(password)) {
		JOptionPane.showMessageDialog(this, "密码不能为空!");
		return;
	}
	Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
	//设置超时时间
	socket.setSoTimeout(3000);
	//构建IO
	OutputStream os = socket.getOutputStream();
	InputStream is = socket.getInputStream();
	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
	BufferedReader br = new BufferedReader(new InputStreamReader(is));
	//传输请求业务+学生id
	bw.write("67" + '\n'+sno+'\n'+pwd+'\n');
	bw.flush();
	//从服务端输入到本地
	String str= br.readLine();
	System.out.println(str);
	System.out.println("传输结束");
	//输出输入流关闭
	os.close();
	socket.close();
	if (str.equals("true")) {
		JOptionPane.showMessageDialog(this, "修改成功!");
	} else {
		JOptionPane.showMessageDialog(this, "修改失败!");
	}

	/*Student student = new Student(Integer.parseInt(sno), password);
	Connection con = null;
	try {
		con = dbUtil.getCon();
		int modifyNum = studentDao.PasswordModify(con, student);
		if (modifyNum == 1) {
			JOptionPane.showMessageDialog(this, "修改成功!");
			this.resetValue();
			this.fillTable(new Student());
		} else {
			JOptionPane.showMessageDialog(this, "修改失败!");
		}
	} catch (Exception e) {
		e.printStackTrace();
		JOptionPane.showMessageDialog(this, "修改失败!");
	} finally {
		try {
			dbUtil.closeCon(con);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}



private void passwordTableMousePressed(java.awt.event.MouseEvent evt) {
	int row = passwordTable.getSelectedRow();
	this.sidTxt.setText((String) passwordTable.getValueAt(row, 0));
	this.snameTxt.setText((String) passwordTable.getValueAt(row, 1));
}


//任意查询方式只要有一个不空即可
private void jb_searchActionPerformed(java.awt.event.ActionEvent evt) throws IOException, ClassNotFoundException {
	String s_sno = this.s_sidTxt.getText();
	Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
	//设置超时时间
	socket.setSoTimeout(3000);
	//构建IO
	OutputStream os = socket.getOutputStream();
	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
	bw.write("69" +'\n'+s_sno+'\n');
	bw.flush();
	//从服务端输入到本地
	InputStream is = socket.getInputStream();
	ObjectInputStream in = new ObjectInputStream(is);
	Object obj = in.readObject();
	System.out.println("传输结束");
	Student student = (Student) obj;
	ArrayList<Student>s=new ArrayList<Student>();
	s.add(student);
	//输出输入流关闭
	is.close();
	os.close();
	socket.close();
	fillTable(s);
}

public static void main(String args[]) {
	java.awt.EventQueue.invokeLater(new Runnable() {
		public void run() {
			try {
				new UserManageFrame_Admin("").setVisible(true);
			} catch (IOException e) {
				throw new RuntimeException(e);
			} catch (ClassNotFoundException e) {
				throw new RuntimeException(e);
			}
		}
	});
}


}
